﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlExtractor.Config;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Dynamic;
using HtmlExtractor;
using HtmlExtractor.Processor;
using System.Reflection;
namespace SlTest
{
    public class MyProcessor : IProcessor
    {
        #region IProcessor Members

        public string GetResult(string text, ParserContext context)
        {
            return null;
        }

        #endregion
    }
   
    [TestClass]
    public class LinqConfigCreaterTest
    {
        [TestMethod]
        public void DynamicProcessor()
        {
            ProcessorFactory.CreateProcessor("SlTest.MyProcessor, SlTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
            
        }
        [TestMethod]
        public void TryCreatePageConfig()
        {
           
            LinqPageConfigCreater ruleCreater = new LinqPageConfigCreater("Resource/CreatePageRule.xml");
            PageConfig pageRule;
            ruleCreater.TryCreatePageConfig(@"http://www.lxwxw.com/xiaoshuoinfo/69/69255.html", out pageRule);
            Assert.AreEqual(pageRule.Count, 5);
            Simple simple1=(Simple)pageRule[0];
            Assert.AreEqual(simple1.Name, "name");
            List<DirectReplace> res = simple1.ReplaceExpressions.Cast<DirectReplace>().ToList();
            Assert.AreEqual(res[0].FindRegex.Expression, "<br/?>");
            Assert.AreEqual(res[0].ReplacementText, ";");
            Assert.AreEqual(res[1].FindRegex.Expression, "<br/?;>");
            Assert.AreEqual(res[1].ReplacementText, ";;");
            Assert.AreEqual(2, res.Count);
            Assert.AreEqual(simple1.ValidExpression.Expression, "valid");
           
            Assert.AreEqual(simple1.Regex.Expression, "Regex");
            Simple subsimple = (Simple)simple1.SubRule;
            Simple subsubsimple = (Simple)subsimple.SubRule;
            Assert.IsNotNull(subsimple);
            Assert.AreEqual(subsimple.Name, "subname");
            Assert.IsNotNull(subsubsimple);
            Assert.AreEqual(subsubsimple.Name, "subsubname");
            //group3
            Group groupRule3 = (Group)pageRule[2];
            Assert.AreEqual(groupRule3.GroupInfos.Count,2);
            //group4
            Group groupRule4 = (Group)pageRule[3];
            Assert.AreEqual(groupRule4.Name, "TestSubGroup");
          
            Assert.AreEqual(groupRule4.Regex.Expression, "TestSubGroupRegex");
            Simple subsimple1 = (Simple)groupRule4.SubRule;
            Assert.IsNotNull(subsimple1);
            Assert.AreEqual(subsimple1.Name, "BookName");
         
            Simple groupsubsimple = (Simple)groupRule4.GroupInfos[0].SubRule;
            Simple groupsubsubsimple = (Simple)groupsubsimple.SubRule;
            Group groupsubsubsubsimple = (Group)groupsubsubsimple.SubRule;
            Assert.IsNotNull(groupsubsimple);
            Assert.AreEqual(groupsubsimple.Name, "BookName");
            Assert.IsNotNull(groupsubsubsimple);
            Assert.AreEqual(groupsubsubsimple.Name, "BookName");
            Assert.IsNotNull(groupsubsubsubsimple);
            Assert.AreEqual(groupsubsubsubsimple.Name, "最新章节");
            Group groupRule5 = (Group)pageRule[4];
            GroupInfo group5info = (GroupInfo)groupRule5.GroupInfos[0];
            List<ReplaceExpression> res2 = group5info.ReplaceExpressions.ToList();
            ProcessedReplace pr = res2[1] as ProcessedReplace;
            Assert.AreEqual(@"<h1.*?>(.*)</H1>", pr.FindRegex.Expression);
            Assert.AreEqual("PageUri", pr.InputPath);
            Assert.AreEqual("bb{0}", pr.FormateReplacementText);
            Assert.IsNotNull(pr.SingleResultMatch);
            //
            Assert.AreEqual(2, res2.Count);
        
        }
    }
}
